home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The PC-SIG Library 10
/
The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso
/
PC_SIGCD
/
22
/
4
/
DISK2247.ZIP
/
CBASE101.ZIP
/
BTREE101.ZIP
/
BTOPEN.C
< prev
next >
Wrap
Text File
|
1990-06-20
|
4KB
|
170 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "@(#)btopen.c 1.4 - 90/06/20" */
#include <blkio.h>
#include <errno.h>
/*#include <stddef.h>*/
/*#include <string.h>*/
/* local headers */
#include "btree_.h"
/* btree control structure table definition */
btree_t btb[BTOPEN_MAX];
/*man---------------------------------------------------------------------------
NAME
btopen - open a btree
SYNOPSIS
btree_t *btopen(filename, type, fldc, fldv)
const char *filename;
const char *type;
int fldc;
const btfield_t fldv[];
DESCRIPTION
The btopen function opens the file named by filename as a btree.
A pointer to the btree_t control structure associated with the
file is returned.
type is a character string having one of the following values:
"r" open for reading
"r+" open for update (reading and writing)
See btcreate for explanation of the field count fldc and the
field definition list fldv.
btopen will fail if one or more of the following is true:
[EINVAL] filename is the NULL pointer.
[EINVAL] type is not "r" or "r+".
[EINVAL] fldc is less than 1.
[EINVAL] fldv is the NULL pointer.
[EINVAL] fldv contains an invalid field definition.
[ENOENT] The named btree file does not exist.
[BTECORRUPT] The named file is corrupt.
[BTEEOF] No file header.
[BTEMFILE] Too many open btrees. The maximum is defined as
BTOPEN_MAX in <btree.h>.
SEE ALSO
btclose, btcreate.
DIAGNOSTICS
On failure btopen returns a NULL pointer, and errno set to
indicate the error.
NOTES
The same field count and field definition list with which the
btree was created must be used each time the btree is opened.
Otherwise the results are undefined.
------------------------------------------------------------------------------*/
btree_t *btopen(filename, type, fldc, fldv)
const char *filename;
const char *type;
int fldc;
const btfield_t fldv[];
{
btree_t * btp = NULL;
int terrno = 0; /* tmp errno */
/* validate arguments */
if (filename == NULL || type == NULL) {
errno = EINVAL;
return NULL;
}
/* find free slot in btb table */
for (btp = btb; btp < btb + BTOPEN_MAX; ++btp) {
if (!(btp->flags & BTOPEN)) {
break; /* found */
}
}
if (btp >= btb + BTOPEN_MAX) {
errno = BTEMFILE; /* no free slots */
return NULL;
}
/* open file */
if (strcmp(type, BT_READ) == 0) {
btp->flags = BTREAD;
} else if (strcmp(type, BT_RDWR) == 0) {
btp->flags = BTREAD | BTWRITE;
} else {
errno = EINVAL;
return NULL;
}
btp->bp = bopen(filename, type, sizeof(bthdr_t), (size_t)1, (size_t)0);
if (btp->bp == NULL) {
if ((errno != ENOENT) && (errno != EACCES)) BTEPRINT;
terrno = errno;
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return NULL;
}
/* load btree_t structure */
if (bgeth(btp->bp, &btp->bthdr) == -1) { /* header */
BTEPRINT;
if (errno == BEEOF) errno = BTEEOF;
else if (errno == BEBOUND) errno = BTECORRUPT;
terrno = errno;
bclose(btp->bp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return NULL;
}
if (btp->bthdr.flags & BTHMOD) { /* corrupt file */
bclose(btp->bp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = BTECORRUPT;
return NULL;
}
if (!bt_fvalid(btp->bthdr.keysize, fldc, fldv)) {
bclose(btp->bp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = EINVAL;
return NULL;
}
btp->fldc = fldc; /* field count */
btp->fldv = NULL;
btp->cbtpos.node = NIL; /* cursor */
btp->cbtpos.key = 0;
btp->cbtnp = NULL;
btp->sp = NULL;
if (bt_alloc(btp) == -1) { /* allocate memory for btree */
BTEPRINT;
terrno = errno;
bclose(btp->bp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return NULL;
}
memcpy(btp->fldv, fldv, btp->fldc * sizeof(*btp->fldv));
/* set up buffering */
if (bsetvbuf(btp->bp, NULL, bt_blksize(btp), BTBUFCNT) == -1) {
BTEPRINT;
terrno = errno;
bt_free(btp);
bclose(btp->bp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return NULL;
}
errno = 0;
return btp;
}